﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recSanctionformCopy : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                recDebtorRepo debtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recDebtor> debtors = debtorDal.GetList(base.SubSubSectorIDs);

                ddlDebtor.Items.Clear();
                debtors.ForEach(delegate(recDebtor debtor)
                {
                    ddlDebtor.Items.Add(new ListItem(debtor.DebtorCode + " - " + debtor.DebtorName, debtor.DebtorID.ToString()));
                });
                ddlDebtor.Items.Insert(0, new ListItem("-- pilih debitur --", string.Empty));

                recSanctionTypeRepo recSanctionTypeRepo = new recSanctionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recSanctionType> sanctiontypes = recSanctionTypeRepo.GetList();

                ddlSanctionType.Items.Clear();
                sanctiontypes.ForEach(delegate(recSanctionType sanctiontype)
                {
                    ddlSanctionType.Items.Add(new ListItem(sanctiontype.SanctionTypeName, sanctiontype.SanctionTypeID.ToString()));
                });

                ddlSanctionType.Items.Insert(0, new ListItem("-- pilih tipe sanksi --", string.Empty));

                if (id > 0) // Edit an existing item
                {
                    recSanctionRepo sanctionDal = new recSanctionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                    recSanction sanction = sanctionDal.GetById(id);
                    if (sanction != null)
                    {
                        recDebtorSectorRepo debtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        recDebtorSector debtorSector = debtorSectorDal.GetById(sanction.DebtorSectorID);

                        if (debtorSector.DebtorID > 0)
                        {
                           // Take Sector,SubSector,SubSubSector based on selected debtorId
                           List<recDebtorSector> debtorSectors =  debtorSectorDal.GetListByDebtor(debtorSector.DebtorID);

                           ddlSubSector.Items.Clear();
                           ddlDebtorSector.Items.Clear();
                           debtorSectors.ForEach(delegate(recDebtorSector sector)
                           {
                               ddlSubSector.Items.Add(new ListItem(sector.recSubSector.SubSectorCode + " - " + sector.recSubSector.SubSectorName, sector.recSubSector.SubSectorID.ToString()));
                               ddlDebtorSector.Items.Add(new ListItem(sector.recSubSubSector.SubSubSectorCode + " - " + sector.recSubSubSector.SubSubSectorName, sector.DebtorSectorID.ToString()));
                           });

                           ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sektor --", string.Empty));
                           ddlDebtorSector.Items.Insert(0, new ListItem("-- pilih sub sub sektor --", string.Empty));

                           if (debtorSector.DebtorID > 0)
                               ddlDebtor.Items.FindByValue(debtorSector.DebtorID.ToString()).Selected = true;

                           if (debtorSector.SubSectorID > 0)
                               ddlSubSector.Items.FindByValue(debtorSector.SubSectorID.ToString()).Selected = true;

                        }

                        hfID.Value = id.ToString();
                        ddlDebtorSector.Items.FindByValue(sanction.DebtorSectorID.ToString()).Selected = true;
                        ddlSanctionType.Items.FindByValue(sanction.SanctionTypeID.ToString()).Selected = true;
                        txtLetterNo.Text = sanction.LetterNo;

                        txtLetterDate.Text = sanction.LetterDate.HasValue ? sanction.LetterDate.Value.ToString("dd/MM/yyyy") : string.Empty;

                        txtValue.Text = sanction.Value.ToString();
                        //.Text = Helper.ToDecimal(sanction.Value.ToString(), NumberStyles.Currency, Helper.Culture.Indonesian).ToString();// sanction.Value.ToString();

                        //Helper.ToDecimal(idInvoices[1].ToString().Trim(), System.Globalization.NumberStyles.Number, Helper.Culture.English); ;// Convert.ToDecimal(idInvoices[1].ToString());

                        txtInvoiceNo.Text = sanction.InvoiceNo;

                        if (sanction.InvoiceDate.HasValue)
                            txtInvoiceDate.Text = sanction.InvoiceDate.Value.ToString("dd/MM/yyyy");

                        txtLetterNo.Text = sanction.LetterNo;
                        
                        if (sanction.LetterDate.HasValue)
                            txtLetterDate.Text = sanction.LetterDate.Value.ToString("dd/MM/yyyy");

                        hfInvoiceNo.Value = sanction.InvoiceNo.Trim();

                        if (!string.IsNullOrEmpty(sanction.InvoiceNo))
                            trInvoice.Visible = true;

                        if (sanction.InvoiceDate.HasValue)
                            trInvoiceDate.Visible = true;

                        if (!string.IsNullOrEmpty(sanction.PaymentNo))
                            trPayment.Visible = true;

                        if (sanction.PaymentDate.HasValue)
                            trPaymentDate.Visible = true;

                    }
                }

                GetPrivilege();
            }
          
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    }
}
